What is null-coalescing operator[??] in C#?
The ?? is called null-coalescing operator which can be used to assign a default value when a nullable type is assigned to a non nullable type. Refer the below code to understand better,
int? n1; //Do some operations int finalvalue = (n1 ?? 10);
In the above code, finalvalue will be assigned with 10 if n1 is null else it will be assigned with the value of n1. In short, finalvalue will hold the left-handside operand if it is not null else it will hold 10.
Also, by default you will not able to assign a nullable type to a non nullable type. In this case, you can use ?? operator by specifying a default value.
Happy Coding!!
|